import { useState, useCallback } from "react"

export const usePlanFilter = ({ dataSource }) => {
    const [filter, setFitler] = useState({ title: '', env: '' })

    const onChangeFilter = useCallback((field, value) => setFitler(currentFilter => ({ ...currentFilter, [field]: value })), [])
    const onChangeFilterTitle = useCallback(title => onChangeFilter('title', title), [])
    const onChangeFilterEnv = useCallback(env => onChangeFilter('env', env.name), [])

    const ifMatchFilter = (plan, { title, env }) => (title ? plan.title.indexOf(title) >= 0 : true) && (env ? plan.env === env : true)
    const visiblePlans = dataSource.filter(suite => ifMatchFilter(suite, filter))      // 过滤后的plan

    return {
        visiblePlans,
        onChangeFilterTitle,
        onChangeFilterEnv,
        filter
    }
}